package ay.shadow.admin.controller;

import ay.shadow.core.utils.JwtTokenProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("api/")
@RequiredArgsConstructor
@RestController
public class LoginController {

    @PostMapping("/login")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> apiLogin(@RequestBody Map<String, String> loginRequest) {
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");

        // 简单的用户验证逻辑（实际项目中应该从数据库验证）
        if ("admin".equals(username) && "admin".equals(password)) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("Authorization", "Bearer " + JwtTokenProvider.generateToken(username,"1",password));
            response.put("message", "登录成功");
            return ResponseEntity.ok(response);
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "用户名或密码错误");
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 处理登出请求的API接口
    @PostMapping("/logout")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> apiLogout() {
        // 清除认证信息（这里可以添加清除token等逻辑）
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("message", "登出成功");

        return ResponseEntity.ok(response);
    }
}
